﻿// ***********************************************************************
// Assembly         : DN3P.Project
// Author           : tianteng
// Created          : 01-12-2017
//
// Last Modified By : tianteng
// Last Modified On : 01-12-2017
// ***********************************************************************
// <copyright file="CurUserGroupUtil.cs" company="北京智帆高科科技有限公司">
//     Copyright © 北京智帆高科科技有限公司2016
// </copyright>
// <summary></summary>
// ***********************************************************************
using DevExpress.XtraTreeList;
using OSS.BIM.Business;
using OSS.BIM.Business.Utility;
using ZFGK.osgEarth.UI.Utility;
using ZFGK.DevExprss.Utility;

namespace OSS.BIM.Utility
{
    /// <summary>
    /// Class CurUserGroupUtil.
    /// </summary>
    public static class CurUserGroupUtil
    {
        /// <summary>
        ///     Gets or sets the tree view model.
        /// </summary>
        /// <value>The tree view model.</value>
        private static TreeList TreeList
        {
            get { return BmTreeListManager.TreeList; }
        }

        /// <summary>
        /// Loads the current feeder.
        /// </summary>
        public static void LoadCurUserGroup()
        {
            CurUserGroup.Instance = ZfEarthCacheUtil.valueCache.LaodCache<CurUserGroup>();
            if (CurUserGroup.Instance == null)
            {
                CurUserGroup.Instance = new CurUserGroup();
            }
            else
            {
                // 判断保存的当前分组是否在数据库中
                if (CurUserGroup.Instance.ModelGrope != null && CurUserGroup.Instance.ModelGrope.IsExit() <= 0)
                {
                    CurUserGroup.Instance.ModelGrope = null;
                }
                if (CurUserGroup.Instance.Project != null && CurUserGroup.Instance.Project.IsExit() <= 0)
                {
                    CurUserGroup.Instance.Project = null;
                }
                SetCurUserGroupEventArgs args = new SetCurUserGroupEventArgs();
                args.SetCur(CurUserGroup.Instance);
                SetCurUserGroup(args);
            }
        }

        /// <summary>
        /// Sets the current feeder event.
        /// </summary>
        /// <param name="args">The <see cref="SetCurUserGroupEventArgs"/> instance containing the event data.</param>
        public static void SetCurUserGroup(SetCurUserGroupEventArgs args)
        {
            TreeList.BeginUpdate();
            try
            {
//                 if (args.OldUserProject != null)
//                     ZfUIManager.GetUserProjectTreeNode(args.OldUserProject).ForeColor = Color.Black;
//                 if (args.OldUserGroup != null)
//                     ZfUIManager.GetUserGroupTreeNode(args.OldUserGroup).ForeColor = Color.Black;

                var mainRibbonForm = BimManagerApp.mainRibbonForm;
                var ribbonPageGroup = mainRibbonForm.ribbonControl
                    .Page("用户模型").Group("当前信息");

                var barProject = ribbonPageGroup.GetBarItem("当前项目");
                barProject.Caption = args.CurUserProject == null ? "当前项目" : "当前项目:" + args.CurUserProject.Name;
                var barUserGroup = ribbonPageGroup.GetBarItem("当前分组");
                barUserGroup.Caption = args.CurUserGroup == null ? "当前分组" : "当前分组:" + args.CurUserGroup.Name;

//                 if (args.CurUserProject != null)
//                 {
//                     // 删除变电站导致的崩溃
//                     var treeNode = ZfUIManager.GetUserProjectTreeNode(args.CurUserProject);
//                     if (treeNode != null)
//                         treeNode.ForeColor = Color.Red;
//                 }
//                 if (args.CurUserGroup != null)
//                 {
//                     var treeNode = ZfUIManager.GetUserGroupTreeNode(args.CurUserGroup);
//                     if (treeNode != null)
//                         treeNode.ForeColor = Color.Red;
//                 }
                ZfEarthCacheUtil.valueCache.SaveCache(CurUserGroup.Instance);
            }
            finally
            {
                TreeList.EndUpdate();
            }
        }
    }
}
